home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_075 / comm / xmdmsend.c < prev   
C/C++ Source or Header  |  1992-05-06  |  4KB  |  174 lines

  1. /* Comm XMODEM send routines */
  2.  
  3. #define  XMDMSEND 1
  4.  
  5. #include "globals.h"
  6. #include <fcntl.h>
  7.  
  8. extern void  emit_tx(), emit_rx(), emits_tx(), emits_rx();
  9. static int   naks,errsect;
  10. static ULONG bytes;
  11.  
  12. XMODEM_Send_File(file)
  13. UBYTE *file;
  14. {
  15.     extern void  purge_line(), send_proto(), report();
  16.     extern int   read_proto(), readchar();
  17.  
  18.     USHORT readstatus, status, timeoutcount;
  19.     UBYTE  ch;
  20.  
  21.     abort = cancel = FALSE;
  22.     report(file);
  23.     if ((fd = open(file, O_RDONLY)) < 0)
  24.         {
  25.         sprintf(sbuff,"\nCannot Open Send File %s\n",file);
  26.         emits_rx(sbuff);
  27.         send_proto(CAN); send_proto(CAN);
  28.         return FALSE;
  29.         }
  30.     else
  31.         emits_rx("Ready to send File -- ESC aborts transfer\n\n");
  32.  
  33.    naks = errsect = bytes = 0;
  34.  
  35.    Xconfig(TRUE);    /* set serial port to 8/N/1  */
  36.    sector = 1;  timeoutcount = 0;
  37.  
  38.    purge_line();     /* flush any garbage in receiver */
  39.    do                /* start handshake with receiver */
  40.       {
  41.          ch = readchar(TTIME,TRUE);
  42.          if(timeout)
  43.            {
  44.              if(++timeoutcount == RETRYMAX)
  45.                {
  46.                   emits_rx("\nNo response, aborting\n");
  47.                   return FALSE;
  48.                }
  49.             }
  50.          if(abort || cancel ) return FALSE;
  51.       } while (ch != NAK && ch != 'C' && ch != 'W');
  52.  
  53.    if(ch == 'W') return WXMODEM_Send_File(file);
  54.  
  55.    status = OK;
  56.    crcflag = (ch == 'C');
  57.    sprintf(sbuff,"%s mode requested\n",crcflag ? "\nCRC" : "\nChecksum");
  58.    emits_rx(sbuff);
  59.  
  60.    while( (readstatus = fillbuf()) && status == OK )
  61.     {                                  /* fill file buffer until EOF */
  62.       if(readstatus == ERROR)          /*  or ERROR during read */
  63.          return FALSE;
  64.       status = sendsector( xbuffer );   /* send sector */
  65.       sprintf(sbuff,"File: %s  bytes= %ld  naks= %d [ %d ]\r",file,bytes,
  66.                   naks,errsect);
  67.       status_line(0,sbuff);
  68.       Process_window_event();
  69.     }
  70.  
  71.    if(status == TIMEOUT || status == ABORT)
  72.       return FALSE;
  73.  
  74.    timeoutcount = 0;
  75.    do
  76.      {
  77.       purge_line();
  78.       send_proto( EOT );     /* end transmission */
  79.       if(timeout)
  80.         if(timeoutcount++ == RETRYMAX || cancel || abort)
  81.           return FALSE;
  82.      } while ((ch = read_proto( TTIME, TRUE )) != ACK);
  83.    close( fd );
  84.    return TRUE;
  85. }
  86.  
  87. int fillbuf()
  88. {
  89.    unsigned int bytes_read;
  90.  
  91.    bytes_read = read(fd, xbuffer, SECSIZ);
  92.  
  93.    if (bytes_read == -1)   /* error during read */
  94.       return ERROR;
  95.    if (bytes_read == 0)    /* end of file */
  96.       return FALSE;
  97.    if(bytes_read < SECSIZ)
  98.       for(; bytes_read < SECSIZ; bytes_read++)
  99.          xbuffer[bytes_read] = PAD;
  100.    return TRUE;
  101. }
  102.  
  103. /**************************************
  104. sendsector() transmits a single sector
  105.  with retransmit on error from receiver
  106. ***************************************/
  107. sendsector( buf )
  108. UBYTE *buf;
  109. {
  110.    extern void do_crc(), purge_line(), send_proto();
  111.    unsigned int i, badblock;
  112.  
  113.    badblock = 0;
  114.    cancel = FALSE;
  115.  
  116.    while( TRUE )
  117.    {
  118.       if( badblock == ERRORMAX )
  119.          return TIMEOUT;
  120.       if( cancel || abort )
  121.          return ABORT;
  122.  
  123.       sprintf(sbuff,"\rTransmitting block %d",sector);
  124.       emits_rx(sbuff);
  125.       send_proto( SOH );          /* start of block */
  126.       send_proto( sector );       /* sector number  */
  127.       send_proto( ~sector );      /* compliment of sector number */
  128.       checksum = crc = 0;       /* clear CRC and checksum */
  129.  
  130.       for( i = 0 ; i < SECSIZ ; i++ )
  131.         {
  132.           if(buf[ i ] == '\n')
  133.             if(asciiflg) buf[ i ] = 0x0D;
  134.           sendchar(buf[ i ]);
  135.           do_crc( buf[ i ] );
  136.           if(viewflg)
  137.             emit_vw( buf[ i ] );
  138.         }
  139.       purge_line();              /* flush garbage from receiver */
  140.       if( crcflag )
  141.       {
  142.          do_crc(0); do_crc(0);   /* cycle CRC generator */
  143.          send_proto( crc >> 8 );   /* High byte */
  144.          send_proto( crc );        /* Low byte */
  145.       }
  146.       else
  147.          send_proto( checksum );
  148.  
  149.       if( read_proto(TTIME, TRUE ) == ACK)
  150.       {
  151.          bytes += SECSIZ;
  152.          break;
  153.       }
  154.       else
  155.       {
  156.          badblock++; naks++;
  157.          errsect = sector;
  158.       }
  159.    }
  160.    sector++;            /* bump sector count   */
  161.    return OK;
  162. }
  163.  
  164. read_proto(time,flag)
  165. int time,flag;
  166. {
  167.    int ch;
  168.  
  169.    ch = readchar(time,flag);
  170.    emit_rx_protocol(ch);
  171.    return (ch);
  172. }
  173.  
  174.